/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.rest;

import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.auction.domain.AuctionProduct;
import co.yixiang.modules.auction.domain.AuctionRecord;
import co.yixiang.modules.auction.domain.YxUserGoods;
import co.yixiang.modules.auction.service.AuctionProductService;
import co.yixiang.modules.auction.service.AuctionRecordService;
import co.yixiang.modules.auction.service.YxUserGoodsService;
import co.yixiang.modules.auction.service.dto.YxUserGoodsDto;
import co.yixiang.modules.auction.service.dto.YxUserGoodsQueryCriteria;
import co.yixiang.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

/**
* @author hupeng
* @date 2021-05-28
*/
@AllArgsConstructor
@Api(tags = "兑换记录管理")
@RestController
@RequestMapping("/api/yxUserGoods")
public class YxUserGoodsController {

    private final YxUserGoodsService yxUserGoodsService;
    private final AuctionRecordService auctionRecordService;
    private final AuctionProductService auctionProductService;
    private final IGenerator generator;
    private final RedisUtils redisUtil;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','yxUserGoods:list')")
    public void download(HttpServletResponse response, YxUserGoodsQueryCriteria criteria) throws IOException {
        yxUserGoodsService.download(generator.convert(yxUserGoodsService.queryAll(criteria), YxUserGoodsDto.class), response);
    }

    @GetMapping
    @Log("查询兑换记录")
    @ApiOperation("查询兑换记录")
    public ResponseEntity<Object> getYxUserGoodss(YxUserGoodsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxUserGoodsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增兑换记录")
    @ApiOperation("新增兑换记录")
    @PreAuthorize("@el.check('admin','yxUserGoods:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YxUserGoods resources){
        return new ResponseEntity<>(yxUserGoodsService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改兑换记录")
    @ApiOperation("修改兑换记录")
    @PreAuthorize("@el.check('admin','yxUserGoods:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YxUserGoods resources){
        yxUserGoodsService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除兑换记录")
    @ApiOperation("删除兑换记录")
    @PreAuthorize("@el.check('admin','yxUserGoods:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            yxUserGoodsService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @Log("审核")
    @ApiOperation("审核")
    @GetMapping("/audit")
    public ResponseEntity<Object> audit(@RequestParam(value = "id") Long id) {
        //修改状态
        YxUserGoods userGoods = yxUserGoodsService.getById(id);
        userGoods.setStatus(2);
        yxUserGoodsService.updateById(userGoods);
        //生成抢拍记录
        AuctionProduct auctionProduct = auctionProductService.getById(userGoods.getPid());
        AuctionRecord auctionRecord = new AuctionRecord();
        Object o = redisUtil.get("dict:sxf");
        auctionRecord.setStartTime(new Date());
        auctionRecord.setUserId(userGoods.getUid().intValue());
        auctionRecord.setPrice(auctionProduct.getPrice());
        auctionRecord.setTopPrice(auctionProduct.getTopPrice());
        auctionRecord.setTimeId(auctionProduct.getTimeId());
        auctionRecord.setAuctionProductId(auctionProduct.getId());
        auctionRecord.setServiceCharge(auctionProduct.getPrice().multiply(new BigDecimal(String.valueOf(o))));
        auctionRecordService.save(auctionRecord);
        redisUtil.set("paimai"+auctionRecord.getId(),auctionRecord.getId());
        auctionRecord.setUpId(auctionRecord.getId());
        auctionRecordService.updateById(auctionRecord);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
